local yangwei = fk.CreateSkill {

  name = "joyex__yangwei",

  tags = { Skill.Compulsory, },

}



yangwei:addEffect(fk.CardUsing, {
  name = "joyex__yangwei",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing,fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardUsing then
      return target == player and player:hasSkill(yangwei.name)  and data.card.trueName == "slash" and player.phase == Player.Play
    else
      return target == player  and player:hasSkill(yangwei.name) and player.phase == Player.Play and player:usedCardTimes("slash", Player.HistoryTurn) > 2 and player:isWounded()
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      local card
      if data.card.color == Card.Black then
        room:setPlayerMark(player,"@joyex__yangwei-phase",{"red"})
      elseif data.card.color == Card.Red then
        room:setPlayerMark(player,"@joyex__yangwei-phase",{"black"})
      else
        room:setPlayerMark(player,"@joyex__yangwei-phase",0)
      end
    else
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = yangwei.name
        })
    end
  end
})
yangwei:addEffect(fk.EventPhaseEnd, {
  name = "joyex__yangwei",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing,fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardUsing then
      return target == player and player:hasSkill(yangwei.name)  and data.card.trueName == "slash" and player.phase == Player.Play
    else
      return target == player  and player:hasSkill(yangwei.name) and player.phase == Player.Play and player:usedCardTimes("slash", Player.HistoryTurn) > 2 and player:isWounded()
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      local card
      if data.card.color == Card.Black then
        room:setPlayerMark(player,"@joyex__yangwei-phase",{"red"})
      elseif data.card.color == Card.Red then
        room:setPlayerMark(player,"@joyex__yangwei-phase",{"black"})
      else
        room:setPlayerMark(player,"@joyex__yangwei-phase",0)
      end
    else
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = yangwei.name
        })
    end
  end
})

yangwei:addEffect("targetmod", {
  name = "#joyex__yangwei_target",
  frequency = Skill.Compulsory,
  bypass_times = function(self, player, skill, scope, card, to)
    return card and card.trueName == "slash" and player:getMark("@joyex__yangwei-phase") ~= 0 and table.contains(player:getTableMark("@joyex__yangwei-phase"), card:getColorString())
  end,
})

return yangwei